package vincent.com.layouttest.anim;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import vincent.com.layouttest.ColorStatusActivity;
import vincent.com.layouttest.R;
import vincent.com.layouttest.util.ImageUtil;
import vincent.com.layouttest.view.RotateImageView;

/**
 * Created by zhengwenchen on 2016/4/22.
 */
public class RotateViewActivity extends ColorStatusActivity {

    private RotateImageView mRotateImgView;
    private Button mControlStateBtn;

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    if(!mRotateImgView.isPaused()){
                        float currentDegree = mRotateImgView.getDegree();
                        mRotateImgView.setDegree(currentDegree+0.306f);
                        mRotateImgView.postInvalidate();

                        mHandler.sendEmptyMessageDelayed(1, 17);
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mRotateImgView = (RotateImageView)findViewById(R.id.rotate_img_view);
        mControlStateBtn = (Button)findViewById(R.id.control_state_btn);

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.my_myalbum_img);
        Bitmap circle = ImageUtil.circleHead(bitmap, bitmap.getWidth(), bitmap.getHeight(), 0, Color.TRANSPARENT);

        mRotateImgView.setImageBitmap(circle);

        try {
            bitmap.recycle();
            bitmap = null;
        }catch (Exception e){
            Log.e("czw_exception", e.getMessage());
        }
    }

    @Override
    public View onCreateView(ViewGroup rootView) {
        return LayoutInflater.from(this).inflate(R.layout.activity_rotate_view, rootView, true);
    }

    @Override
    public String getToolbarTitle() {
        return "Rotate View";
    }

    public void controlState(View v){
        if(mRotateImgView.isPaused()){
//            mRotateImgView.setPause(false);
            mControlStateBtn.setText("Stop");

//            mHandler.sendEmptyMessage(1);
            mRotateImgView.start();
        }else{
//            mRotateImgView.setPause(true);
            mControlStateBtn.setText("Start");
            mRotateImgView.stop();
        }
    }

    public void resetState(View v){
        mRotateImgView.reset();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mRotateImgView.stop();
    }
}
